package com.woorigil.android.koreaflower.activity.product.list;

import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.woorigil.android.koreaflower.AppContext;
import com.woorigil.android.koreaflower.activity.product.ProductDetailActivity;
import com.woorigil.android.koreaflower.domain.product.Product;

abstract public class ProductListFragment extends Fragment implements OnItemClickListener, OnScrollListener {
//	protected LayoutInflater layoutInflater;
	protected Context context;
	protected ProgressDialog dataLoadindDialog;
	protected ProductListAdapter productListViewAdapter;
	protected ListView productListView;
	protected int lastSize;
	protected boolean isAttachLock = false;
	protected boolean isMoreView = false;
	protected LinearLayout dealListHeaderView;
	protected LinearLayout dealListFooterView;
	protected List<Product> deals;
	
	
	abstract public int getPosition();
	abstract public int getDealsSize();
	abstract public void getDataFromServer(Context context);
	abstract public void drawBodyWhenInit();
	
	public void drawHeader() {
		productListView.addHeaderView(dealListHeaderView);
	}
	
	public void drawBodyWhenMore(List<Product> newDeals) {
		for (Product each : newDeals) {
			productListViewAdapter.add(each);
		}
		productListViewAdapter.notifyDataSetChanged();
	}
	
	public void drawFooter(Context context, int currCount) {
		if (currCount < AppContext.getDealListLimitCount(context)) {
			productListView.addFooterView(dealListFooterView);
		}
	}	
	
	public ProductListAdapter getDealListAdapter() {
		return productListViewAdapter;
	}
	
	protected void changePosition(final int position) {
		productListView.clearFocus();
		productListView.post(new Runnable() {
		    @Override
		    public void run() {
		    	productListView.requestFocusFromTouch();
		    	productListView.setSelection(position);
		    	productListView.requestFocus();
		    }
		});
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Product product = productListViewAdapter.getItem(position - productListView.getHeaderViewsCount());
		
		Intent intent = new Intent(context, ProductDetailActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
		intent.putExtra("product", product);
		startActivity(intent);
	}
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (isAttachLock) {
	    	return;
	    }
		
		if (productListView.getFooterViewsCount() > 0) {
			return;
		}
		
		if (totalItemCount > 0 && firstVisibleItem + visibleItemCount == totalItemCount) {
			isMoreView = true;
			getDataFromServer(view.getContext());
	    }  
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}	
}
